package com.wswy.wzcx.ui.car.result

import android.arch.lifecycle.Observer
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.che.libcommon.ui.VOMessage
import com.che.libcommon.ui.loading.ILoadingView
import com.wswy.wzcx.R
import com.wswy.wzcx.model.license.UserCarInfo
import com.wswy.wzcx.module.GlobalConfigManager
import com.wswy.wzcx.module.base.CBaseLoadingFragment
import com.wswy.wzcx.ui.Constants
import kotlinx.android.synthetic.main.fragment_fine_history.*

class HistoryFinesFragment : CBaseLoadingFragment() {

    companion object{

        @JvmStatic
        fun newInstance(userCarInfo: UserCarInfo):HistoryFinesFragment{
            return HistoryFinesFragment().apply {

                arguments = Bundle().apply {
                    putParcelable(Constants.EXTRA_DATA,userCarInfo)
                }
            }
        }

    }

    override fun getConfigure(): ILoadingView.LoadingConfigure = object : ILoadingView.SampleLoadingConfigure() {
        override fun getOnRetryClickListener(): View.OnClickListener {
            return View.OnClickListener {
                showLoading(true)

            }
        }
    }

    override fun getFragmentTitle():String{


        return arguments?.getParcelable<UserCarInfo?>(Constants.EXTRA_DATA)?.carNo?:""
    }

    override fun getLoadingFrameId(): Int {
        return R.id.fl_content
    }

    override fun getContentLayoutId(): Int {
        return R.layout.fragment_fine_history
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        arguments?.getParcelable<UserCarInfo?>(Constants.EXTRA_DATA)?.let {

            loadData(it)

        }?:kotlin.run {

            finish()

        }



    }

    private fun loadData(userCarInfo: UserCarInfo){

        recycler_view.layoutManager = LinearLayoutManager(requireContext())

        val queryResultAdapter= QueryResultAdapter()


        recycler_view.adapter = queryResultAdapter

        val finesViewModel = provideViewModelFromActivity(FinesViewModel::class.java)

        finesViewModel.getFineHistoryLiveData().observe(this, Observer {

            it?.runCatching {


                if (isOk()){

                    showData()

                    if (data.isNullOrEmpty()){

                        showError(VOMessage.createEmpty(R.drawable.mycar_empty_default, R.string.empty_history_fines, 0))

                    }else{

                        tv_fine_count.text = getString(R.string.fine_complete_count,data.size)

                        queryResultAdapter.setItems(data)

                        GlobalConfigManager.getInstance().adsConf.getWzcxAd()?.let {
                            queryResultAdapter.setAdModel(it)
                        }

                        queryResultAdapter.notifyDataSetChanged()
                    }



                }else if (inLoading()){
                    showLoading(true)
                }else if (hasError()){
                    showError(VOMessage.create(null))
                }

            }

        })

        finesViewModel.loadHistory(userCarInfo.carNo)
    }

}